package com.menker.app.util;

import android.app.Activity;
import android.text.TextUtils;

import com.ali.auth.third.core.MemberSDK;
import com.ali.auth.third.core.callback.LoginCallback;
import com.ali.auth.third.core.model.Session;
import com.ali.auth.third.login.LoginService;
import com.alibaba.baichuan.android.trade.AlibcTrade;
import com.alibaba.baichuan.android.trade.AlibcTradeSDK;
import com.alibaba.baichuan.android.trade.AlibcUrlCenter;
import com.alibaba.baichuan.android.trade.model.AlibcShowParams;
import com.alibaba.baichuan.android.trade.model.OpenType;
import com.alibaba.baichuan.android.trade.page.AlibcBasePage;
import com.alibaba.baichuan.android.trade.page.AlibcDetailPage;
import com.alibaba.baichuan.android.trade.page.AlibcMyCartsPage;
import com.alibaba.baichuan.android.trade.page.AlibcMyOrdersPage;
import com.alibaba.baichuan.android.trade.page.AlibcPage;
import com.alibaba.baichuan.trade.biz.AlibcTradeBiz;
import com.alibaba.baichuan.trade.biz.context.AlibcTradeShowParam;
import com.alibaba.baichuan.trade.biz.core.taoke.AlibcAidCompoment;
import com.alibaba.baichuan.trade.biz.core.taoke.AlibcTaokeParams;
import com.alibaba.baichuan.trade.biz.login.AlibcLogin;
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback;
import com.alibaba.baichuan.trade.biz.utils.AlibcTradeHelper;
import com.alimama.tunion.trade.TUnionTradeSDK;
import com.alimama.tunion.trade.convert.TUnionJumpType;
import com.alimama.tunion.trade.convert.TUnionMediaParams;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.menker.app.common.ToolbarOwner;
import com.menker.app.common.Utils;
import com.menker.app.ui.MenkerTradeCallback;
import com.menker.app.webview.CustomWebChromeClient;
import com.menker.app.webview.GeneralWebViewClient;

import java.util.HashMap;
import java.util.Map;

public class TaobaoUtils {

    private static AlibcShowParams mAlibcShowParams = new AlibcShowParams(OpenType.H5, false);
    private static AlibcTaokeParams mAlibcTaokeParams = new AlibcTaokeParams();
    private static Map<String, String> mExParams = new HashMap<>();
    static
    {
        mAlibcTaokeParams.setPid("mm_123917702_38468147_147062648");
        mExParams.put("isv_code", "appisvcode");
        mExParams.put("alibaba", "阿里巴巴");//自定义参数部分，可任意增删改
    }

    public static boolean isItemDetailsLink(String url) {
        if (!TextUtils.isEmpty(url)) {
            url = url.toLowerCase().trim();
            if (url.contains("item.html?id=") || (url.contains("item.html") && url.contains("&id="))
                    || url.contains("item.htm?id=") || (url.contains("item.htm") && url.contains("&id="))
                    || url.contains("detail.html?id=") || (url.contains("detail.html") && url.contains("&id="))
                    || url.contains("detail.htm?id=") || (url.contains("detail.htm") && url.contains("&id="))
                    ) {
                return true;
            }
        }

        return false;
    }

    public static boolean isInvalidLink(String url) {
        if (TextUtils.isEmpty(url)) {
            return true;
        }

        url = url.toLowerCase().trim();
        return (url.startsWith("tbopen://")
                || url.startsWith("tmall://")
                || url.startsWith("taobao://")
                || url.startsWith("taobaotravel://")
                || url.startsWith("https://v.asczwa.com/goto")
                || url.startsWith("itms-apps://")
                || url.startsWith("about:blank")
                || url.contains("itunes.apple.com")
                || url.contains("downloadtaobao.html")
                || url.contains("ul_redirect=tmall")
        );
    }

    public interface TaobaoLoginCallback {
        void onResult(boolean result);
    }

    public static String getTaobaoAccountName() {
        AlibcLogin alibcLogin = AlibcLogin.getInstance();

        String name = "";

        if (alibcLogin.isLogin()) {
            name = alibcLogin.getSession().nick;
            if (TextUtils.isEmpty(name)) {
                name = "无名账户";
            }
        }

        return name;
    }

    public static boolean isTaobaoLogin() {
        AlibcLogin alibcLogin = AlibcLogin.getInstance();
        return alibcLogin.isLogin();
    }

    public static void loginTaobao(final TaobaoLoginCallback taobaoLoginCallback) {
//        LoginService service = (LoginService) MemberSDK.getService(LoginService.class);
//        service.setLoginCallback(new LoginCallback() {
//            @Override
//            public void onSuccess(Session session) {
//                //
//            }
//
//            @Override
//            public void onFailure(int code, String message) {
//                Utils.UI.showToastLong(message);
//            }
//        });

        AlibcLogin alibcLogin = AlibcLogin.getInstance();

        alibcLogin.showLogin(new AlibcLoginCallback() {
            @Override
            public void onSuccess(int i) {
                taobaoLoginCallback.onResult(true);
            }
            @Override
            public void onFailure(int code, String msg) {
                Utils.UI.showToastLong(msg);
                taobaoLoginCallback.onResult(false);
            }
        });
    }

    public static void logoutTaobao() {
        AlibcLogin alibcLogin = AlibcLogin.getInstance();
        alibcLogin.logout(new AlibcLoginCallback() {
            @Override
            public void onSuccess(int i) {
                int x = 0;
            }
            @Override
            public void onFailure(int i, String msg) {
                Utils.UI.showToastLong(msg);
            }
        });
    }

    public static void showTaobaoPage(String pageId, String pageParams, Activity activity, ToolbarOwner.IToolbar iToolbar, BridgeWebView webView) {
        if ("myOrder".equals(pageId)) {
            showOrder(activity,iToolbar,webView);
        }else if ("myCart".equals(pageId)) {
            showCart(activity,iToolbar,webView);
        }else if ("myFav".equals(pageId)) {
//            showFav(activity,iToolbar,webView);
        }else if ("productDetail".equals(pageId)) {
            showDetail(pageParams, activity, iToolbar, webView);
        }
    }

    private static void showOrder(Activity activity, ToolbarOwner.IToolbar iToolbar, BridgeWebView webView) {
        AlibcBasePage alibcBasePage = new AlibcMyOrdersPage(0, true);
        if (activity != null) {
            AlibcTrade.show(activity, webView,new GeneralWebViewClient(activity, iToolbar, null, webView), new CustomWebChromeClient(activity, iToolbar),
                    alibcBasePage, mAlibcShowParams, mAlibcTaokeParams, mExParams, new MenkerTradeCallback());
        }
    }

    private static void showCart(Activity activity, ToolbarOwner.IToolbar iToolbar, BridgeWebView webView) {
        AlibcBasePage alibcBasePage = new AlibcMyCartsPage();
        if (activity != null) {
            AlibcTrade.show(activity, webView,new GeneralWebViewClient(activity, iToolbar, null, webView), new CustomWebChromeClient(activity, iToolbar),
                    alibcBasePage, mAlibcShowParams, mAlibcTaokeParams, mExParams, new MenkerTradeCallback());
        }
    }

    /**
     * 商品详情页
     * @param itemId 支持itemId和openItemId的商品，必填，不允许为null；
     *               eg.37196464781L；AAHd5d-HAAeGwJedwSnHktBI；
     */
    private static void showDetail(String itemId, Activity activity, ToolbarOwner.IToolbar iToolbar, BridgeWebView webView) {
        if (!TextUtils.isEmpty(itemId)) {
            AlibcBasePage alibcBasePage;
//            itemId = "522166121586";
            alibcBasePage = new AlibcDetailPage(itemId);
            if (activity != null) {
                AlibcTrade.show(activity, webView,new GeneralWebViewClient(activity, iToolbar, null, webView), new CustomWebChromeClient(activity, iToolbar),
                        alibcBasePage, mAlibcShowParams, mAlibcTaokeParams, mExParams, new MenkerTradeCallback());
            }
        }
    }

    public static void showUrl(String url, Activity activity, ToolbarOwner.IToolbar iToolbar, BridgeWebView webView) {
        AlibcPage alibcPage = new AlibcPage(url);
        if (activity != null) {
            AlibcTrade.show(activity, webView, new GeneralWebViewClient(activity, iToolbar, url, webView), new CustomWebChromeClient(activity, iToolbar),
                    alibcPage, mAlibcShowParams, mAlibcTaokeParams, mExParams, new MenkerTradeCallback());
        }
    }

}
